/*
 * Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.resource.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import ca.ioniq.server.resource.OrderResource;
import ca.ioniq.server.service.OrderManagementService;
import ca.ioniq.server.service.exception.OrderException;
import ca.ioniq.shared.entity.Order;

public class OrderResourceImpl implements OrderResource {

    private OrderManagementService orderManagementService;

    @Inject
    public OrderResourceImpl(OrderManagementService orderManagementService) {
        this.orderManagementService = orderManagementService;
    }

	public Order get(Long id) {
		return orderManagementService.getOrder(id);
	}

	public Order put(Order order) {
		return orderManagementService.putOrder(order);
	}
	
	public void cancelOrder(Long id) {
		orderManagementService.cancelOrder(id);
	}

	public void update(Order order) {
		try {
			orderManagementService.updateOrder(order);
		} catch (OrderException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}		
	}

	public List<Order> viewAllOrdersByCompany(Long companyId) {
		return orderManagementService.viewAllOrdersByCompany(companyId);
	}

	public List<Order> viewLastOrdersByCompany(String date, Long companyId) {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date callDate;
		try {
			callDate = formatter.parse(date);
		} catch (ParseException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}
		return orderManagementService.viewLastOrdersByCompany(callDate, companyId);
	}

	public List<Order> viewOrdersByUser(Long userId) {
		return orderManagementService.viewOrdersByUser(userId);
	}


}
